const db = uniCloud.database();
const dbCmd = db.command;
module.exports = async function(options = {}) {
	const { groupCode, characterId } = options;
	const token = this.getUniIdToken();
	if (!groupCode || !characterId) {
		throw new Error('非法访问');
	}
	const [groupList, applyList] = await Promise.all([
		db.collection('a-dz-group-list').where({ groupCode }).get({ getOne: true }),
		db.collection('a-dz-group-apply-list').where({ groupCode, applyStatus: 'AGREE' }).get({ getOne: true }),
	])
	if (groupList.data.length) {
		const group = groupList.data[0];
		//保证创建角色才能解散
		if (group.characterId !== characterId) {
			throw new Error('非法操作');
		}
		if (applyList.data.length) {
			return {
				code: -3,
				msg: '该群组还有成员，请先移除'
			}
		}
		await Promise.all([
			db.collection('a-dz-group-list').where({ groupCode }).remove(),
			db.collection('a-dz-group-apply-list').where({ groupCode }).remove()
		])
		return {
			code: 0,
			msg: '解散成功'
		}
	} else {
		throw new Error('非法访问');
	}
}